﻿using DoNet.Common.Helpers;
using DoNet.Common.Result;
using DoNet.Wechat.MP.Common;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace WechatMp.Web.Controllers.API.MP
{
    [Route("MP/[controller]")]
    [ApiController]
    public class CommonController : ApiMPController
    {
        private readonly CommonApi commonApi;

        public CommonController(ILogger<HomeController> _logger, IServiceProvider _serviceProvider) : base(_logger, _serviceProvider)
        {
            commonApi = new CommonApi(_logger, _serviceProvider);
        }

        [HttpGet]
        [Route("GetAccessToken")]
        public IActionResult GetAccessToken(string appid, string secret)
        {
            /*
            string appid = "wxeaa2e9243ecd7e8c";
            string secret = "061b61c47afa6680cf89531d3ac1c6";
            */
            /*
            测试号信息
            string appid = "wx52d8883b88a4f717";
            string secret = "9fd91a73c284597f94b842b9f7ab8f26";
            */
            var result = accessTokenMP.GetAccessToken(appid, secret);
            return Content(JsonHelper.SerializeObject(result));
        }

        [HttpGet]
        [Route("GetApiDomainIp")]
        public IActionResult GetApiDomainIp(string appid, string secret)
        {
            CommonResult result = new CommonResult();
            var accessTokenResult = accessTokenMP.GetAccessToken(appid, secret);
            if (accessTokenResult.Success)
            {
                var accessToken = accessTokenResult.ResData?.ToString();
                if (!string.IsNullOrWhiteSpace(accessToken))
                {
                    var resultData = commonApi.GetApiDomainIp(accessToken);
                    if (resultData != null)
                    {
                        if (resultData.errcode == 0)
                        {
                            result.Success = true;
                            result.ResData = new {
                                ip_list = resultData.ip_list
                            };
                        }
                        result.ErrCode = resultData.errcode.ToString();
                        result.ErrMsg = resultData.errmsg;
                    }
                }
            }
            else
            {
                result.ErrMsg = accessTokenResult.ErrMsg;
                result.ResData = accessTokenResult.ResData;
            }
            return ToJsonContent(result);
        }

        [HttpGet]
        [Route("GetCallBackIp")]
        public IActionResult GetCallBackIp(string appid, string secret)
        {
            CommonResult result = new CommonResult();
            var accessTokenResult = accessTokenMP.GetAccessToken(appid, secret);
            if (accessTokenResult.Success)
            {
                var accessToken = accessTokenResult.ResData?.ToString();
                if (!string.IsNullOrWhiteSpace(accessToken))
                {
                    var resultData = commonApi.GetCallBackIp(accessToken);
                    if (resultData != null)
                    {
                        if (resultData.errcode == 0)
                        {
                            result.Success = true;
                            result.ResData = resultData;
                        }
                        result.ErrCode = resultData.errcode.ToString();
                        result.ErrMsg = resultData.errmsg;
                    }
                }
            }
            else
            {
                result.ErrMsg = accessTokenResult.ErrMsg;
                result.ResData = accessTokenResult.ResData;
            }
            return ToJsonContent(result);
        }

        [HttpGet]
        [Route("CallBackCheck")]
        public IActionResult CallBackCheck(string appid, string secret)
        {
            CommonResult result = new CommonResult();
            var accessTokenResult = accessTokenMP.GetAccessToken(appid, secret);
            if (accessTokenResult.Success)
            {
                var accessToken = accessTokenResult.ResData?.ToString();
                if (!string.IsNullOrWhiteSpace(accessToken))
                {
                    var resultData = commonApi.CallBackCheck(accessToken, "all", "DEFAULT");
                    if (resultData != null)
                    {
                        if (resultData.errcode == 0)
                        {
                            result.Success = true;
                            result.ResData = resultData;
                        }
                        result.ErrCode = resultData.errcode.ToString();
                        result.ErrMsg = resultData.errmsg;
                    }
                }
            }
            else
            {
                result.ErrMsg = accessTokenResult.ErrMsg;
                result.ResData = accessTokenResult.ResData;
            }
            return ToJsonContent(result);
        }
    }
}
